package com.chance.cc.crawler.prod.command.job.domain.vm.xcar.supplement;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.xcar.XCarAutoCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.internalDownload;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Tag_Site_Info;

/**
 * @author lt
 * @version 1.0
 * @date 2021-05-18 14:22:45
 * @email okprog@sina.com
 */
public class XCarPriseSimpleCrawlerSchedulerJob extends XCarAutoCommonCrawlerSchedulerJob {
    public static String site = "publicPraise";
    /**
     * carSeries sgmkw bmw-kw adhocKw
     *
     */
    public static String metaSite = "sgmkw";

    public static void main(String[] args) {
        publishXcarNewsSchedulerJob();
    }

    public static CrawlerJob publishXcarNewsSchedulerJob(){
        CrawlerJob crawlerJob = xcarCwalerJob();


        //发布定时采集作业
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("爱卡汽车车系口碑临时采集");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage page = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布作业：" + page.getRawText());

        return crawlerJob;
    }

    /**
     * taobao采集
     * @return
     */
    public static CrawlerJob xcarCwalerJob() {

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl("https://newcar.xcar.com.cn/auto/index.php")
                .httpHead("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36")
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(StringUtils.joinWith("-",filter,domain,site,"realtime")))
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(0,new long[]{1629216000000L,System.currentTimeMillis()}))
//                .proxy(proxy)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);//脚本中指定publicPraise
        requestRecord.tagsCreator().bizTags().addSiteBiz("praise");//脚本中指定praise
        requestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,"sgm-20210818-20211104");

        //初始 comment crawler request record
        String requestQueueName = StringUtils.joinWith("-","crawler",domain,site,"simple","queue");
        CrawlerRequestRecord initNewsCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("xcar_series",turnPageItem)
                .httpUrl(metaServiceHttpPrefix + "/v1/meta/xcar/keys?site=" + metaSite)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();

        CrawlerJob crawlerJob = CrawlerJob.builder()
                .crawlerJobThreadNumber(20)
                .triggerInfo(
                        domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-", domain, site,"simple", CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .kafkaResultPipeline(null, kafkaTopciForTraceJob, null)
                .fileResultPipeline(null,"/data/chance_crawler_runner/logs/node/xcar/praise_simple.log",false)
                .requestRecord(requestRecord)
                .supportRecord(initNewsCrawlerRecord)
                .build();
        return crawlerJob;
    }
}
